import 'package:json_annotation/json_annotation.dart';
import 'package:flutter_account_book/utils/db/db_helper.dart';

part 'account_record.g.dart';

@JsonSerializable()
class AccountRecord extends DbModel {
  @JsonKey(required: true)
  late String id = "";
  // 金额
  @JsonKey(required: true)
  late double amount = 0.0;
  // 记录时间
  @JsonKey(required: true)
  late int recordTime = DateTime.now().millisecondsSinceEpoch;
  @JsonKey(required: false)
  late int? createTime;
  @JsonKey(required: false, disallowNullValue: false)
  late int? updateTime;
  // 支付方式
  @JsonKey(required: false, defaultValue: "")
  late String payMethod = "";
  @JsonKey(required: false, defaultValue: 0)
  late int payMethodId = 0;
  // 记录类型
  @JsonKey(required: false, defaultValue: "")
  late String recordType = "";
  @JsonKey(required: false, defaultValue: 0)
  late int recordTypeId = 0;
  // 备注
  @JsonKey(required: false)
  late String? remark;
  // 账本索引
  @JsonKey(required: false, defaultValue: 0)
  late int books = 0;
  // 0支出 1收入
  @JsonKey(required: true, defaultValue: 0)
  int isIncome = 0;

  AccountRecord() : super.fromJson({});

  factory AccountRecord.fromJson(data) => _$AccountRecordFromJson(data);

  @override
  Map<String, dynamic> toJson() => _$AccountRecordToJson(this);

  Future<void> save() async {
    bool has = await DbHelper.helper.accountRecordDao.has(id);
    if (!has) {
      await DbHelper.helper.accountRecordDao.insert(this);
    } else {
      await DbHelper.helper.accountRecordDao.update(this);
    }
  }
}
